import { login, userInfo, logout } from '@/api/login'
import { getToken, setToken, removeToken,clearTokenAndStore } from '@/common/auth'
import axios from 'axios'
import { MessageBox } from 'element-ui';
import axiosUtil from '../../utils/axiosUtil'
const SET_TOKEN = 'SET_TOKEN'
const SET_NAME = 'SET_NAME'
const SET_AGE = 'SET_AGE'
const SET_AVATAR = 'SET_AVATAR'
const SET_PERMISSIONS = 'SET_PERMISSIONS'
const SET_USERID='SET_USERID'
const SET_SOCKET='SET_SOCKET'
const user = {
  state: {
    token: getToken(),
    name: '',
    age: 0,
    avatar: '',
    permissions: '',
    userId:'',
    websocket:{}
  },
  mutations: {
    [SET_SOCKET](state,websocket){
      state.websocket=websocket;
    },
    [SET_USERID](state,userId){
      state.userId=userId
    },
    [SET_TOKEN](state, token) {
      state.token = token
    },
    [SET_NAME](state, name) {
      state.name = name
    },
    [SET_AGE](state, age) {
      state.age = age
    },
    [SET_AVATAR](state, avatar) {
      state.avatar = avatar
    },
    [SET_PERMISSIONS](state, permissions) {
      state.permissions = permissions
    }
  },
  actions: {
    // 用户登录
    login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(resp => {
          let data = resp.data
          setToken(data.token)
          commit(SET_TOKEN, data.token)
          return resolve()
        }).catch(err => {
          return reject(err)
        })
      })
    },
    // 拉取用户信息
     refreshUserByToken({ commit,getters }) {
      return new Promise((resolve, reject) => {
        if (process.env.NODE_ENV === 'production'){
          axios.get("http://127.0.0.1/ai-face-system/ai_face/ai-face-attendance/user/refreshUserByToken",{
            //参数列表
            params:{},
            //请求头配置
            headers:{ token: sessionStorage.getItem("token") }
          }).then((res)=>{
            let data = res.data.data;
            commit(SET_NAME, data.userName)
            commit(SET_AGE, data.age)
            commit(SET_AVATAR, data.avatar)
            commit(SET_PERMISSIONS, data.permissionCode)
            commit(SET_USERID,data.userId)
            let url=process.env.NODE_ENV === 'development'?'localhost/ai-face-system/socket':'localhost/ai-face-system/socket'
            //初始化websocket
            let websocket=new WebSocket(`ws:///${url}/${getToken()}`);
            websocket.onopen=function () {
              console.log('socket连接，可以开始发送消息了')
            }
            // //socket收到消息
            // websocket.onmessage=function (evt) {
            //   console.log('来自服务端的消息'+evt.data)
            // }
            commit(SET_SOCKET,websocket)
            return resolve(data)
          })
        }else{
          axiosUtil.$ajaxGet('/user/refreshUserByToken').then(resp => {
            let data = resp
            commit(SET_NAME, data.userName)
            commit(SET_AGE, data.age)
            commit(SET_AVATAR, data.avatar)
            commit(SET_PERMISSIONS, data.permissionCode)
            commit(SET_USERID,data.userId)
            let url=process.env.NODE_ENV === 'development'?'localhost/ai-face-system/socket':'localhost/ai-face-system/socket'
            //初始化websocket
            let websocket=new WebSocket(`ws:///${url}/${getToken()}`);
            websocket.onopen=function () {
              console.log('socket连接，可以开始发送消息了')
            }
            // //socket收到消息
            // websocket.onmessage=function (evt) {
            //   console.log('来自服务端的消息'+evt.data)
            // }
            commit(SET_SOCKET,websocket)
            return resolve(data)
          }).catch(err => {
            return reject(err)
          })
        }

      })
    },
    // 用户退出登录
    logout({ commit }) {
      return new Promise((resolve, reject) => {
        logout().then(resp => {
          clearTokenAndStore();
          return resolve()
        }).catch(err => {
          return reject(err)
        })
      })
    }
  },
  getters: {
    token: state => state.token,
    name: state => state.name,
    age: state => state.age,
    avatar: state => state.avatar,
    permissions: state => state.permissions,
    userId:state=>state.userId,
    socket:state=>state.websocket
  }
}

export default user
